<template>
  <div class="login-container">
    <el-card
      ><div class="title"></div>
      <el-form ref="form" label-width="80px" :model="user" :rules="rules">
        <el-form-item label="用户名" prop="name">
          <el-input prefix-icon="el-icon-user" placeholder="" v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="psd">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            placeholder=""
            show-password
            v-model="user.psd"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repsd">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            placeholder=""
            show-password
            v-model="user.repsd"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" width="100%" @click="reg">注册</el-button>
      <el-link :underline="false" @click="$router.push('/login')">已有账号，去登录</el-link>
    </el-card>
  </div>
</template>

<script>
import { regAPI } from '@/api/user'
export default {
  name: 'MyRegist',
  data() {
    const validataPass2 = (rule, value, callback) => {
      if (value === this.user.psd) {
        callback()
      } else {
        callback(new Error('两次密码输入不一致！请重新输入！'))
      }
    }
    return {
      user: {
        name: '',
        psd: '',
        repsd: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/,
            message: '字母开头，允许5-16字节，允许字母数字下划线',
            trigger: 'blur'
          }
        ], // 用户名的验证规则
        psd: [
          { required: true, message: '请输入正确的密码格式', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z]\w{5,17}$/,
            message: '以字母开头，长度在6~18之间，只能包含字母、数字和下划线)',
            trigger: 'blur'
          }
        ], // 密码的验证规则
        repsd: [
          { required: true, message: '请再次输入密码，并确保两次密码一致', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z]\w{5,17}$/,
            message: '以字母开头，长度在6~18之间，只能包含字母、数字和下划线)',
            trigger: 'blur'
          },
          { validator: validataPass2, trigger: 'blur' }
        ] // 确认密码的验证规则
      }
    }
  },
  methods: {
    reg() {
      // form 是 <el-form> 的ref属性值，必须有这个值
      this.$refs.form.validate(async valid => {
        // console.log(valid) // 验证通过valid=true;  验证不通过valid=false
        if (!valid) return // 验证不通过，则return
        // console.log('验证通过，可以提供数据了')
        const data = {
          username: this.user.name,
          password: this.user.psd,
          repassword: this.user.repsd
        }
        const { data: res } = await regAPI(data)

        // console.log(res)

        if (res.code === 0) {
          // 注册成功：提示、重置表单、跳转到登录页
          this.$message.success(res.message)
          this.$refs.form.resetFields() // 重置表单
          this.$router.push('/login') // 跳转到登录页
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style>
.login-container {
  background-image: url('@/assets/images/login_bg.jpg');
  background-size: cover;
  height: 100%;
}
.el-card {
  width: 400px;
  height: 400px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.el-form-item {
  margin-top: 35px;
}
.title {
  height: 40px;
  background: url('@/assets/images/login_title.png') top center no-repeat;
  /* background-size: cover; */
}
.el-button {
  margin-top: 10px;
  width: 100%;
}
/* .el-link {
  display: flex;
  justify-content: center;
  align-items: center;
} */
</style>
